"use strict";

const BaseModel = require("../basics/mysql/mapper");
class HomeModel extends BaseModel {
  constructor(cloud) {
    super(cloud, "prize_stock");
  }

  /**
   * 获取实例
   * @param {*} cloud 
   */
  static getInstant(cloud) {
    return new HomeModel(cloud);
  }



  /**
   * 查询单条
   * @param {*} today 
   * @param {*} code 
   */
  async selectOne(today, code) {
    const ex = this.createExample();
    ex.criteria.andEqual("today", today).andEqual("code", code);
    return await this.selectOneByExample(ex);
  }

  /**
   * 查询列表
   */
  async selectList(today) {
    const ex = this.createExample();
    ex.criteria.andEqual("today", today);
    return await this.selectListByExample(ex);
  }


  /**
   * 减
   * @param {*} id 
   */
  async reduce(id) {
    const sql = `update prize_stock set stock=stock-1 where id=${id} and stock>0`;
    const data = await this.mfSql(sql, []);
    if (data.affectedRows < 1) {
      return false;
    }
    return true;
  }


  /**
   * 回滚
   * @param {*} id 
   */
  async back(id) {
    const sql = `update prize_stock set stock=stock+1 where id=${id} and stock<total`;
    const data = await this.mfSql(sql, []);
    if (data.affectedRows < 1) {
      return false;
    }
    return true;
  }
}

module.exports = HomeModel;